package jp.co.showbiz.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * 機 能 ： オークション共通メッソト 種 別 ： 日付関数 説 明 ： 新規作成 作 成 日 ： 2006/11/22 作 成 者 ： nzh
 **/
public class CommUtil {

    /**
     * メソッド概要<br>
     * メソッド説明<br>
     * @param curPage c
     * @param totalCount t
     * @param pageSize p
     * @param row r
     * @return pageMap
     */
    public static HashMap<String, Integer> getPageInfo(int curPage, int totalCount, int pageSize, int row) {
        HashMap<String, Integer> pageMap = new HashMap<String, Integer>();
        int pageCount = 0;
        int startPage = 0;
        int prevPage = 0;
        int nextPage = 0;
        if (curPage == 0) {
            curPage = 1;
        }
        int skip = (curPage - 1) * pageSize;
        pageCount = totalCount % pageSize;
        if (pageCount == 0) {
            pageCount = totalCount / pageSize;
        } else {
            pageCount = totalCount / pageSize + 1;
        }
        startPage = ((curPage - 1) / row) * row + 1;
        int endPage = (startPage + row) - 1;
        if (endPage > pageCount) {
            endPage = pageCount;
        }
        if (startPage > row) {
            prevPage = startPage - 1;
        }
        if (endPage < pageCount) {
            nextPage = startPage + row;
        }
        pageMap.put("curPage", curPage);
        pageMap.put("skip", skip);
        pageMap.put("pageCount", pageCount);
        pageMap.put("startPage", startPage);
        pageMap.put("endPage", endPage);
        pageMap.put("prevPage", prevPage);
        pageMap.put("nextPage", nextPage);
        pageMap.put("totalCount", totalCount);

        return pageMap;
    }
    
    public static void copyFile(File oldFile, File newFile) throws Exception {
    	copyFile(oldFile, newFile, true);
    }
    
    public static void copyFile(File oldFile, File newFile, boolean deleteFlg) throws Exception {
    	BufferedInputStream bi = null;
    	BufferedOutputStream bo = null;
    	if (oldFile == null) return;
    	try {
    		bi = new BufferedInputStream(new FileInputStream(oldFile));
    		bo = new BufferedOutputStream(new FileOutputStream(newFile));
    		int byteRead = 0;
    		byte[] buffer = new byte[1024];
    		while ((byteRead = bi.read(buffer, 0, 1024)) != -1) {
    			bo.write(buffer, 0, byteRead);
    		}
    	} catch (Exception e) {
    		throw e;
    	} finally {
    		if (bi != null) bi.close();
    		if (bo != null) bo.close();
    		if (deleteFlg) oldFile.delete();
    	}
    }
    
    public static void main(String[] args) throws Exception {
    	File oldFile = new File("C:/showbiz/Hanyeseul/thumb_F.JPG");
    	File newFile = new File("C:/showbiz/Hanyeseul/temp/thumb_F.JPG");
    	copyFile(oldFile, newFile, false);
    }
    
    public static String convertDocumentToString(Document doc) throws TransformerException {
    	TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        //create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        trans.transform(source, result);
        String xmlString = sw.toString();
        return xmlString;
    }
    
    public static String deleteContentLinkPath(String contents) {
    	List<String> aTags = new ArrayList<String>();
    	Pattern a = Pattern.compile("<a[^<>]*>");
    	Matcher m = a.matcher(contents);
    	while (m.find()) {
    		String aTag = contents.substring(m.start(), m.end());
    		aTags.add(aTag);
    	}
    	for (String aTag : aTags) {
    		contents = contents.replace(aTag, "<a href=\"#\">");
    	}
        return contents;
    }
}